import json
from datetime import datetime
from pathlib import Path
import hashlib

class User:
    def __init__(self):
        self.user_id = None
        self.username = None
        self.password_hash = None
        self.age = None
        self.avg_cycle_length = None
        self.phone_number = None
        self.wechat_id = None
        self.period_records = []
        
    def create_user(self, username, password, age, avg_cycle_length, phone_number=None, wechat_id=None):
        self.username = username
        self.password_hash = self._hash_password(password)
        self.age = age
        self.avg_cycle_length = avg_cycle_length
        self.phone_number = phone_number
        self.wechat_id = wechat_id
        self.user_id = hashlib.md5(username.encode()).hexdigest()
        
    def _hash_password(self, password):
        return hashlib.sha256(password.encode()).hexdigest()
    
    def verify_password(self, password):
        return self.password_hash == self._hash_password(password)
    
    def add_period_record(self, start_date):
        self.period_records.append({
            'start_date': start_date.strftime('%Y-%m-%d'),
            'recorded_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        })
        
    def to_dict(self):
        return {
            'user_id': self.user_id,
            'username': self.username,
            'password_hash': self.password_hash,
            'age': self.age,
            'avg_cycle_length': self.avg_cycle_length,
            'phone_number': self.phone_number,
            'wechat_id': self.wechat_id,
            'period_records': self.period_records
        } 